package com.winterchen.controller;

import com.winterchen.model.UserDomain;
import com.winterchen.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * Created by Administrator on 2017/8/16.
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("")
    public String toUserPage() throws Exception{
        return "user/user";
    }
    @ResponseBody
    @PostMapping("/add")
    public int addUser(UserDomain user) throws Exception{
        return userService.addUser(user);
    }

    @ResponseBody
    @GetMapping("/all")
    public Object findAllUser(@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
                              @RequestParam(name = "pageSize", required = false, defaultValue = "10") int pageSize) throws Exception{
        return userService.findAllUser(pageNum,pageSize);
    }

    @ResponseBody
    @PostMapping("/delete")
    public int delete(UserDomain user) throws Exception{
        return userService.deleteById(user);
    }

    /*
    * @Description: 先添加一条数据,然后根据这个id删除这条数据,如果出现异常,应回滚事务
    * @param
    * @Return:
    * @Author: 张峻豪
    * @Date: 2018/9/12 0012 15:50
    */

    @ResponseBody
    @PostMapping("/addAndDelete")
    // @Transactional
    public int addAndDelete(UserDomain user) throws Exception{
        return userService.addAndDelete(user);
    }


}
